package com.itheima.leetcode.od.b.bf;

import java.util.Arrays;

/**
 * <h3>最长公共后缀</h3>
 */
public class LongestCommonPrefix {
    public static void main(String[] args) {
        /*Scanner in = new Scanner(System.in);
        String[] strs = in.nextLine().replace("[", "").replace("]", "").replace("\"", "").split(",");*/

        String[] strs = Arrays.stream("[\"abc\",\"bbc\",\"c\"]"
                        .replace("[", "")
                        .replace("]", "")
                        .replace("\"", "")
                        .split(","))
                .map(s -> new StringBuilder(s).reverse().toString())
                .toArray(String[]::new);
        System.out.println(getResult(strs));
    }

    private static String getResult(String[] strs) {
        char[] first = strs[0].toCharArray();
        for (int i = 0; i < first.length; i++) {
            char c = first[i];

            for (int j = 1; j < strs.length; j++) {
                if (strs[j].length() == i || strs[j].charAt(i) != c) {
                    if (i == 0) {
                        return "@Zero";
                    } else {
                        return new StringBuilder(strs[0].substring(0, i)).reverse().toString();
                    }
                }
            }
        }
        return new StringBuilder(strs[0]).reverse().toString();
    }
}
